// miniprogram/pages/buy/buy.js
const db=wx.cloud.database();
var switc=true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
    search: [],
    
    showbox:true,
    value:'',
    box:'',
    curNav:1,
    imgUrls_one: [
      'https://img14.360buyimg.com/n7/jfs/t14986/352/2472641492/22108/c69ad842/5a9f9920N8fd58933.jpg',
      'https://cbu01.alicdn.com/img/ibank/2018/034/075/10152570430_1264173863.310x310.jpg',
      'http://img3.guilinlife.com/2shou/2013/0303/20130303093704951.jpg'
    ],
    imgUrls_two: [
      'https://www.tengtubz.com/uploads/allimg/180611/%E4%B8%8A%E6%B5%B7pvc%E5%8C%96%E5%A6%86%E5%93%81%E5%8C%85%E8%A3%85%E7%9B%92-3.jpg',
      'https://www.hdjiaohe.com/wp-content/uploads/2020/02/3K8A02791.jpg',
      'https://www.hdjiaohe.com/wp-content/uploads/2020/02/IMG_1905-300x300.jpg'
    ],
    imgUrls_three: [
      'https://img.zcool.cn/community/01f805553089dd000000c5003f31ea.jpg@1280w_1l_2o_100sh.jpg',
      'https://shejidaxue.com/wp-content/uploads/2020/01/Square-Metal-Food-Container-Mockup.jpg',
      'http://www.boxin168.com/UploadFiles/FCK/2017-10/6364479947251225006831158.jpg'
    ],

  },
  onSearch:function(event) {
    // Toast('搜索' + this.data.value);
     let that=this
    console.log(event.detail)
    if(that.data.search==""){
      db.collection('paperbox').where({
      name: event.detail
    }).get({
      success: function(res) {
        that.setData({
          search: res.data,
        })
        switc = false
        console.log('搜索成功', that.data.search)
        console.log('第一次'+switc)
         //第二次搜索
   if(that.data.search==""){
    db.collection('metalbox').where({
   name: event.detail
 }).get({
   success: function(res) {
     that.setData({
       search: res.data,
     })
     switc = false
 console.log('搜索成功', that.data.search)
 console.log('第二次' + switc)
 //第三次搜索
 if(that.data.search==""){
     db.collection('glassbox').where({
   name: event.detail
 }).get({
   success: function(res) {
     that.setData({
       search: res.data,
     })
     switc = false
 console.log('搜索成功', that.data.search)
 console.log('第三次' + switc)
 //第四次搜索
  if(that.data.search==""){
    db.collection('plasticbox').where({
   name: event.detail
 }).get({
   success: function(res) {
     that.setData({
       search: res.data,
     })
   }
 })
 switc = false
 console.log('搜索成功', that.data.search)
 console.log('第四次' + switc)
 if (that.data.search == "") {
   wx.showToast({
     title: '未找到服务',
     icon: "none"
   })
 }
}
   }
 })
 
 }
     
   }
 })
 
  }
      }
    })   
   
   // }
 // }
}
    
  },

  switchRightTab:function(e){
    let id = e.target.dataset.id;
    console.log(id);
    this.setData({
    curNav: id
    })
  },
   
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    db.collection('paperbox').get().then(res=>{
      this.setData({
        box:res.data
      })
    })
    db.collection('plasticbox').get().then(res=>{
      this.setData({
        plasticbox:res.data
      })
    })
    db.collection('metalbox').get().then(res=>{
      this.setData({
        metalbox:res.data
      })
    })
    db.collection('glassbox').get().then(res=>{
      this.setData({
        glassbox:res.data
      })
    })
   

  },
  onClick(){
    this.setData({
      showbox:false,
      value:""
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})